package com.team.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.team.dao.MessageDao;
import com.team.dao.ProjDao;
import com.team.entity.Message;
import com.team.entity.Project;
import com.team.entity.User;
import com.team.util.Const;
import com.team.util.TUtil;

public class NewStateServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("_m");
		if("select".equals(method)){
			select(request,response);
		}else if("save".equals(method)){
			save(request,response);
		}
	
	}
	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
	  System.out.println("save message is exectue");
	  
		String projId = request.getParameter("projId");
		String info = request.getParameter("info");
		
		int projectId = Integer.parseInt(projId);
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
		
		System.out.println(info);
		if(info == null || info ==""){
			System.out.println("null or kong");
			response.sendRedirect("newState.jsp");
			return ;
		}
		
		
		MessageDao mdao = new MessageDao();
		Message mess = new Message();
		mess.setContent(info);
		mess.setCreateTime(TUtil.getNow());
		mess.setUserId(user.getId());
		mess.setProjId(projectId);
		
		int num = mdao.save(mess);
		if(num > 0){
			/*List<Message> list = mdao.selectAllByProjectId(projectId);
			ServletContext application = getServletContext();
			application.setAttribute(Const.APPLICAION_MESSAGE, list);
			response.sendRedirect("newState.jsp");
			return ;*/
			response.sendRedirect("info.jspx?_m=select&projId="+projectId);
			
		}else {
			System.out.println("save info fail");
			response.sendRedirect("newState.jsp");
			return ;
		
		
	}
	}
	private void select(HttpServletRequest request, HttpServletResponse response) throws IOException {
		System.out.println("select is execute");
		
		String projId = request.getParameter("projId");
		int projectId = Integer.parseInt(projId);
		
		ProjDao pd =  new ProjDao();
		MessageDao mdao = new MessageDao();
		
		Project project = pd.selectById(projectId);
		HttpSession session = request.getSession();
		session.setAttribute(Const.SESSION_PROJ, project);
		System.out.println("projId put in session succ");
		
		List<Message> list = mdao.selectAllByProjId(projectId);
		ServletContext application = getServletContext();
		application.setAttribute(Const.APPLICAION_MESSAGE, list);
		response.sendRedirect("newState.jsp");
		return ;		
	}
}
